<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use DateTimeInterface;

class Usersdomain extends Model
{
    protected $table = 'users_domain';
    protected $primaryKey = 'id';

    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format($this->dateFormat ?: 'm月d日H点i分');
    }

    public static function userDefaultDomain($openid)
    {
        return self::select("domain")->where("openid",$openid)->where("disable",1)->where("active",2)->orderBy("id","DESC")->first();
    }

    public static function userDomain($openid)
    {
        return self::select("domain")->where("openid",$openid)->where("disable",1)->orderBy("id","DESC")->first();
    }
    
    public static function userDomainList($openid)
    {
        return self::select("domain","active","status","deadline","created_at")->where("openid",$openid)->where("disable",1)->orderBy("id","DESC")->get();
    }

    public static function domainDeadline($deadline)
    {
        return self::select("id","domain")->where("deadline",$deadline)->orderBy("id","DESC")->get();
    }

    public static function userDomainCount($openid)
    {
        return self::where("openid",$openid)->where("disable",1)->count("id");
    }

    public static function userSendDomain($openid)
    {
        return self::where("openid",$openid)->where("status",1)->count("id");
    }

    public static function domainListActive($openid)
    {
        return self::where("openid",$openid)->update(["active"=>1]);
    }

    public static function domainListDisable($id,$disable)
    {
        return self::where("id",$id)->update(["disable"=>$disable]);
    }

    public static function domainSetActive($openid,$domain)
    {
        return self::where("openid",$openid)->where("domain",$domain)->update(["active"=>2]);
    }

    public static function usersDomainAdd($openid,$domain,$status,$deadline)
    {
        return self::insert(["openid"=>$openid,"domain"=>$domain,"status"=>$status,"deadline"=>$deadline,"created_at"=>time()]);
    }
}